package com.example.demo;

import org.ly.uap.client.authentication.AuthenticationFilter;
import org.ly.uap.client.session.SingleSignOutFilter;
import org.ly.uap.client.session.SingleSignOutHttpSessionListener;
import org.ly.uap.client.util.HttpServletRequestWrapperFilter;
import org.ly.uap.client.util.TransNameFilter;
import org.ly.uap.client.validation.Cas20ProxyReceivingTicketValidationFilter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.boot.context.embedded.InitParameterConfiguringServletContextInitializer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletContextInitializer;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
public class DemoApplication implements ServletContextInitializer {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}


	@Override
	public void onStartup(ServletContext servletContext)
			throws ServletException {
		servletContext.setInitParameter("serverName", "http://www.test.com");
	}


	@Bean(name = "singlesgnoutfilter")
	public SingleSignOutFilter getSingleSignOutFilter(){
		return new SingleSignOutFilter();
	}

	@Bean
	public FilterRegistrationBean helloWorldFilter(@Qualifier("singlesgnoutfilter")SingleSignOutFilter ss) {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		String errUrl = "login.html";
		registration.setFilter(ss);
		registration.addUrlPatterns("/*");
		registration.setName("Single Sign Out Filter");
		registration.setOrder(0);
		return registration;
	}

	@Bean
	public ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> setStartupServletContextListener(){
		ServletListenerRegistrationBean<SingleSignOutHttpSessionListener> result = new ServletListenerRegistrationBean<SingleSignOutHttpSessionListener>();
		result.setListener(new SingleSignOutHttpSessionListener());
//		result.setOrder(1);
		return result;
	}


	@Bean(name = "authenticationFilter")
	public AuthenticationFilter getAuthenticationFilter(){
		return new AuthenticationFilter();
	}

	@Bean
	public FilterRegistrationBean helloWorldFilter2(@Qualifier("authenticationFilter")AuthenticationFilter ss) {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		String Url = "https://cas.yulinu.edu.cn/lyuapServer/login";
		registration.setFilter(ss);
		registration.addUrlPatterns("/*");
		registration.addInitParameter("casServerLoginUrl", Url);
		registration.setName("Authentication Filter");
		registration.setOrder(2);
		return registration;
	}

	@Bean(name = "cas20ProxyReceivingTicketValidationFilter")
	public Cas20ProxyReceivingTicketValidationFilter getCas20ProxyReceivingTicketValidationFilter(){
		return new Cas20ProxyReceivingTicketValidationFilter();
	}

	@Bean
	public FilterRegistrationBean helloWorldFilter3(@Qualifier("cas20ProxyReceivingTicketValidationFilter")Cas20ProxyReceivingTicketValidationFilter ss) {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		String Url = "https://cas.yulinu.edu.cn/lyuapServer";
		registration.setFilter(ss);
		registration.addUrlPatterns("/*");
		registration.addInitParameter("casServerUrlPrefix", Url);
		registration.setName("Validation Filter");
		registration.setOrder(3);
		return registration;
	}


	@Bean(name = "httpServletRequestWrapperFilter")
	public HttpServletRequestWrapperFilter getHttpServletRequestWrapperFilter(){
		return new HttpServletRequestWrapperFilter();
	}

	@Bean
	public FilterRegistrationBean helloWorldFilter4(@Qualifier("httpServletRequestWrapperFilter")HttpServletRequestWrapperFilter ss) {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(ss);
		registration.addUrlPatterns("/*");
		registration.setName("HttpServletRequest Wrapper Filter");
		registration.setOrder(4);
		return registration;
	}

	@Bean(name = "transNameFilter")
	public TransNameFilter getTransNameFilter(){
		return new TransNameFilter();
	}

	@Bean
	public FilterRegistrationBean helloWorldFilter5(@Qualifier("transNameFilter")TransNameFilter ss) {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(ss);
		registration.addUrlPatterns("/*");
		registration.setName("TransNameFilter");
		registration.setOrder(5);
		return registration;
	}

}
